package com.juyu.miliao.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.juyu.miliao.api.domain.UserVideo;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.api.service.HomeVideoService;
import com.juyu.miliao.api.service.OpenApiService;
import com.juyu.miliao.common.domain.BaseQuery;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.OpenApiStatus;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.DataValidateException;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author jh
 * 2018/7/23 10:10
 */
@RestController
@RequestMapping(value = "/home/video")
public class HomeVideoController {

    @Resource(name = "homeVideoService")
    private HomeVideoService homeVideoService;
    @Resource
    private OpenApiService openApiService;
    @Resource
    private AppKeyService appkeyService;

    /**
     * 首页推荐和最新视频
     *
     * @param auth      auth
     * @param baseQuery 分页
     * @param type      1.推荐 2.最新
     * @return list
     */
    @GetMapping(value = "/{pageNum}-{pageSize}")
    public List<UserVideo> recommend(
            HttpServletRequest request,
            Authentication auth,
            BaseQuery baseQuery,
            @PathVariable("pageNum") String pageNum,
            @PathVariable(value = "pageSize", required = false) String pageSize,
            @RequestParam Integer type) {
        Long userId = null;
        if (auth != null) {
            Subject subject = (Subject) auth.getPrincipal();
            userId = subject == null ? null : subject.getId();
        }

        CommonHead com = VerifyUtil.verifyCommonHead(request);

        String videoList = "";
        //审核时给假数据,先判断前端审核,再判断后端审核
        OpenApiStatus ops = openApiService.getOpenApi(com);
        if (ops != null && ops.getForwardAudit() && ops.getBackAudit()) {
            if (!pageNum.equals("1"))
                return new ArrayList<>();
            if (type == 1) {
                videoList = appkeyService.getAppKeyByKey("video_recommend").getValue();
            } else {
                videoList = appkeyService.getAppKeyByKey("video_new").getValue();
            }
            return JSONArray.parseArray(videoList, UserVideo.class);
        }
        if (Objects.equals(type, 1)) {
            return homeVideoService.listHomeVideo(userId, baseQuery, true, com);
        } else if (Objects.equals(type, 2)) {
            return homeVideoService.listHomeVideo(userId, baseQuery, false, com);
        } else {
            throw new DataValidateException("type为1或2");
        }

    }

}
